/*
Copyright (C) 2012  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package OtherDialogs;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;

import Settings.*;
import Storage.*;
import GuiTools.*;

/**
 * Displays all vocabularies being changed to be selected for saving.
 * @author Christoph Schnack
 *
 */
public class SaveDialog {
	JDialog dialog;
	Storage MAINVOC;
	Settings SET;
	Language LANG;
	String frameTitle;
	private boolean CANCELED = true;
	Vector<JCheckBox> toDelete = new Vector<JCheckBox>(), edited = new Vector<JCheckBox>();
	Vector<Vector<JCheckBox>> allCBs = new Vector<Vector<JCheckBox>>();
	
	public SaveDialog(Storage mv,Settings set,String frameTitleArg){
		MAINVOC = mv;
		SET = set;
		LANG = set.getLanguage();
		frameTitle = frameTitleArg;
		buildGUI();
	}
	
	public SaveDialog(Storage mv,Settings set){
		MAINVOC = mv;
		SET = set;
		LANG = set.getLanguage();
		frameTitle = LANG.getSave();
		buildGUI();
	}
	
	private void buildGUI(){
		dialog = new JDialog();
		dialog.setTitle(frameTitle);
		dialog.setModal(true);
		
		allCBs.add(toDelete);
		allCBs.add(edited);
		
		final JCheckBox allCB = new JCheckBox(LANG.getAll());
		allCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				for(Vector<JCheckBox> vec : allCBs){
					if(vec.size()>0) for(JCheckBox cb : vec) cb.setSelected(allCB.isSelected());
				}
			}
		});
		
		JPanel editedPanel = new JPanel();
		editedPanel.setLayout(new BoxLayout(editedPanel,BoxLayout.Y_AXIS));
		for(Vocabulary voc : MAINVOC.getVocabularies()){
			if(voc.getChanged()){
				edited.add(new JCheckBox(voc.getName()));
				editedPanel.add(edited.lastElement());
			}
		}
		
		JPanel toDeletePanel = new JPanel();
		toDeletePanel.setLayout(new BoxLayout(toDeletePanel,BoxLayout.Y_AXIS));
		try{
			File dir = new File(GuiTools.TRIBUITHOME+File.separator+"vocs");
			String[] fileList = dir.list(new FilenameFilter(){
				public boolean accept(File d, String name){
					return name.endsWith(".voc");
				}
			});
			for(String name : fileList){
				name = name.replaceAll(".voc","");
				if(!MAINVOC.hasVocabulary(name)){
					toDelete.add(new JCheckBox(name));
					toDeletePanel.add(toDelete.lastElement());
				}
			}
		}
		catch(Exception e){}
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10,10,10,10);
		
		gbc.gridy++;
		centerPanel.add(new JLabel(LANG.getSaveWhichChanges(),JLabel.LEFT),gbc);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		
		gbc.gridy++;
		centerPanel.add(allCB,gbc);
		
		Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
		if(toDelete.size()>0){
			LabelPanel tdLP = new LabelPanel(LANG.getDeleted()+":",toDeletePanel, font);
			gbc.gridy++;
			centerPanel.add(tdLP,gbc);
		}
		if(edited.size()>0){
			LabelPanel tdLP = new LabelPanel(LANG.getChanged()+":",editedPanel, font);
			gbc.gridy++;
			centerPanel.add(tdLP,gbc);
		}
		
		JButton okBtn = new JButton("ok");
		okBtn.addActionListener(new OkBtnListener());
		JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.addActionListener(new CancelBtnListener());
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);
		
		dialog.getContentPane().add(BorderLayout.NORTH, new Header(LANG.getSaveChanges()));
		dialog.getContentPane().add(BorderLayout.CENTER, centerPanel);
		dialog.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		dialog.setLocation(100,100);
		dialog.pack();
		dialog.setVisible(true);
	}
	
	public boolean getCanceled(){
		return CANCELED;
	}
	
	private class OkBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			for(Vector<JCheckBox> vector : allCBs){
				if(vector.size()>0){
					for(JCheckBox cb : vector){
						if(cb.isSelected()){
							MAINVOC.saveVocabulary(MAINVOC.getVocabulary(cb.getText()));
						}
					}
				}
			}
			CANCELED = false;
			dialog.dispose();
			
		}
	}
	
	private class CancelBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			dialog.dispose();
		}
	}
}
